from fastapi import FastAPI,Query,Path
from pydantic import BaseModel,Field
from typing import Union,List,Annotated

app = FastAPI()


@app.get('/items/{item_id}')
async def read_items(
    item_id: Annotated[int, Path(title="The ID of the item to get")], #annotated就是为了给类型添加元数据
    q: Annotated[str | None, Query(alias="item-query")] = None,
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results

@app.get('/items/{item_id}')  #上下两个等价
async def read_items(
    item_id: int=Path(title="The ID of the item to get"), #annotated就是为了给类型添加元数据
    q: str | None=Query(alias="item-query",default=None),
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results



class FilterParams(BaseModel):
    limit: int = Field(100, gt=0, le=100)
    offset: int = Field(0, ge=0)
    tags: list[str] = ["abc"]


@app.get("/items1/")  #用模型作为类型，只能单独写，如果用Url传参,因为是get，但是其他就不会，put,post
async def read_items(
    filter_query: Annotated[FilterParams, Query()]
):
    return {
        **filter_query.model_dump()
    }